/*
 * Copyright (C) 2010 Seapeak.Xu
 *
 * fostmq may be copied only under the terms of the GNU General
 * Public License V3, which may be found in the fostmq source kit.
 *
 * Created on: 2010-7-27
 * Author: Seapeak.Xu
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "fostmq_conf.h"
#include "fost_propreader.h"
#include "fost_string.h"
#include "fost_typedef.h"

char g_bindip[IP_LEN];
int g_port = 5035;
int g_timeout = 30;
int g_connections = 500;
int g_threadpools = 20;
char g_logpath[PATH_LEN];
char g_storepath[PATH_LEN];
int g_synctime = 5;
int g_chucksize = 100;
int g_nonleafnodes = 1024;
char g_logprefix[PATH_LEN];
int g_loglines = 20000;
int g_loglevel = 4;
int g_stacksize = 256;
int g_daemon = 1;
int g_namedqueue_message_size = 100000000;
int g_namedqueue_size = 256;


#ifndef CONF_BINDIP
#define CONF_BINDIP "bindip"
#endif

#ifndef CONF_PORT
#define CONF_PORT "port"
#endif

#ifndef CONF_TIMEOUT
#define CONF_TIMEOUT "timeout"
#endif

#ifndef CONF_CONNECTIONS
#define CONF_CONNECTIONS "connections"
#endif

#ifndef CONF_THREADPOOLS
#define CONF_THREADPOOLS "threadpools"
#endif

#ifndef CONF_LOGPATH
#define CONF_LOGPATH "logpath"
#endif

#ifndef CONF_STOREPATH
#define CONF_STOREPATH "storepath"
#endif

#ifndef CONF_SYNCTIME
#define CONF_SYNCTIME "synctime"
#endif

#ifndef CONF_CHUCKSIZE
#define CONF_CHUCKSIZE "chucksize"
#endif

#ifndef CONF_NONLEAFNODES
#define CONF_NONLEAFNODES "nonleafnodes"
#endif

#ifndef CONF_LOGPREFIX
#define CONF_LOGPREFIX "logprefix"
#endif

#ifndef CONF_LOGLINES
#define CONF_LOGLINES "loglines"
#endif

#ifndef CONF_LOGLEVEL
#define CONF_LOGLEVEL "loglevel"
#endif

#ifndef CONF_STACKSIZE
#define CONF_STACKSIZE "stacksize"
#endif

#ifndef CONF_DAEMON
#define CONF_DAEMON "daemon"
#endif

#ifndef CONF_NAMEDQUEUE_MESSAGE_SIZE
#define CONF_NAMEDQUEUE_MESSAGE_SIZE "namedqueue_message_size"
#endif

#ifndef CONF_NAMEDQUEUE_SIZE
#define CONF_NAMEDQUEUE_SIZE "namedqueue_size"
#endif

int set_conf(const char *key, const char *value)
{
	if (0 == strcasecmp(key, CONF_BINDIP))
	{
		set_string(g_bindip, value, "");
		return 0;
	}
	if (0 == strcasecmp(key, CONF_PORT))
	{
		set_int(&g_port, value, 5035);//decause my home tele number over with 5035
		return 0;
	}
	if (0 == strcasecmp(key, CONF_CHUCKSIZE))
	{
		set_int(&g_chucksize, value, 100);
		return 0;
	}
	if (0 == strcasecmp(key, CONF_LOGLEVEL))
	{
		set_int(&g_loglevel, value, 4);
		return 0;
	}
	if (0 == strcasecmp(key, CONF_LOGLINES))
	{
		set_int(&g_loglines, value, 20000);
		return 0;
	}
	if (0 == strcasecmp(key, CONF_LOGPATH))
	{
		set_string(g_logpath, value, "/tmp/fostmq/log");
		return 0;
	}
	if (0 == strcasecmp(key, CONF_LOGPREFIX))
	{
		set_string(g_logprefix, value, "fostmq");
		return 0;
	}
	if (0 == strcasecmp(key, CONF_CONNECTIONS))
	{
		set_int(&g_connections, value, 500);
		return 0;
	}
	if (0 == strcasecmp(key, CONF_THREADPOOLS))
	{
		set_int(&g_threadpools, value, 1000);
		return 0;
	}
	if (0 == strcasecmp(key, CONF_TIMEOUT))
	{
		set_int(&g_timeout, value, 30);
		return 0;
	}
	if (0 == strcasecmp(key, CONF_NONLEAFNODES))
	{
		set_int(&g_nonleafnodes, value, 1024);
		return 0;
	}
	if (0 == strcasecmp(key, CONF_STOREPATH))
	{
		set_string(g_storepath, value, "/opt/fostmq/data");
		return 0;
	}
	if (0 == strcasecmp(key, CONF_SYNCTIME))
	{
		set_int(&g_synctime, value, 5);
		return 0;
	}
	if(0 == strcasecmp(key,CONF_STACKSIZE))
	{
		set_int(&g_stacksize,value,256);
		return 0;
	}
	if(0 == strcasecmp(key,CONF_DAEMON))
	{
		set_int(&g_daemon,value,1);
		return 0;
	}
	if(0 == strcasecmp(key,CONF_NAMEDQUEUE_MESSAGE_SIZE))
	{
		set_int(&g_namedqueue_message_size,value,100000000);
		return 0;
	}
if(0 == strcasecmp(key,CONF_NAMEDQUEUE_SIZE))
	{
		set_int(&g_namedqueue_size,value,256);
		return 0;
	}

	return 0;
}
